#include "stdafx.h"

#pragma once

int Memcmp(const void * buf1, const void * buf2, size_t count)
{
	if(!count)
	{
		return;
	}

	while(--count && (*(char *)buf1 == *(char *)buf2))
	{
		buf1 = (char *)buf1 + 1;
		buf2 = (char *)buf2 + 1;
	}

	return (*(unsigned char *)buf1 - *(unsigned char *)buf2);
}

void * Memcpy(void * dst, const void * src, size_t count)
{
	void * ret = dst;

	while(--count)
	{
		*(char *)dst = *(char *)src;
		src = (char *)src + 1;
		dst = (char *)dst + 1;
	}

	return ret;
}

void * Memmove(void * dst, const void * src, size_t count)
{
	void * ret = dst;

	if(dst <= src || (char *)dst >= (char *)src + count)
	{
		while(count--)
		{
			*(char *)dst = *(char *)src;
			src = (char *)src + 1;
			dst = (char *)dst + 1;
		}
	}
	else
	{
		src = (char *)src + count - 1;
		dst = (char *)dst + count - 1;

		while(count--)
		{
			*(char *)dst = *(char *)src;
			src = (char *)src - 1;
			dst = (char *)dst - 1;
		}
	}

	return ret;
}
